package com.tarena.lbs.business.dao.repository.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tarena.lbs.business.dao.mapper.BusinessMapper;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Slf4j
public class BusinessRepositoryImpl implements BusinessRepository {

    @Autowired
    private BusinessMapper businessMapper;

    @Override
    public PageInfo<BusinessPO> getPages(BusinessQuery query) {
        //1.先利用query中非空属性 凭借查询sql的where条件
        QueryWrapper<BusinessPO> queryWrapper=assembleBusinessQuery(query);
        //2.开启分页插件
        PageHelper.startPage(query.getPageNo(), query.getPageSize());
        //3.查询业务列表 被mybatis拦截 查询分页
        List<BusinessPO> pos = businessMapper.selectList(queryWrapper);
        return new PageInfo<>(pos);
    }

    private QueryWrapper<BusinessPO> assembleBusinessQuery(BusinessQuery query) {
        QueryWrapper<BusinessPO> queryWrapper=new QueryWrapper<>();
        //1.businessName 商家名称 模糊查询 非空 where business_name like(%#{}%)
        if (StringUtils.isNotBlank(query.getBusinessName())){
            queryWrapper.like("business_name",query.getBusinessName());
        }
        //2.businessHeadPhone 商家负责人电话 相等查询 非空 where business_head_phone=#{}
        if (StringUtils.isNotBlank(query.getBusinessHeadPhone())){
            queryWrapper.eq("business_head_phone",query.getBusinessHeadPhone());
        }
        //3.businessStatus 商家状态 0 禁用 1正常 2待审核 3未审核 4 5 相等查询 非空 business_status=#{}
        if (query.getBusinessStatus()!=null){
            queryWrapper.eq("business_status",query.getBusinessStatus());
        }
        //4.startTime 商家入住开始时间 非空 entry_time>#{}
        if (query.getStartingTime()!=null){
            //gt方法表示> greater than gte greater than and equal
            //lt方法表示< less than lte less than and equal
            queryWrapper.gt("entry_time",query.getStartingTime());
        }
        //5.endTime 商家入住结束时间 非空 entry_time<#{}
        if (query.getEndTime()!=null){
            queryWrapper.lt("entry_time",query.getEndTime());
        }
        //6.businessId 商家id 相等查询 非空 id=#{}
        if (query.getBusinessId()!=null){
            queryWrapper.eq("id",query.getBusinessId());
        }
        return queryWrapper;

    }

    @Override
    public BusinessPO selectById(Integer businessId) {
        BusinessPO po = businessMapper.selectById(businessId);
        return po;
    }

    @Override
    public Integer save(BusinessPO bpo) {
        log.info("持久层接受到保存商家信息任务：{}",bpo);
        // 插入数据并返回生成的主键ID
        Integer result = businessMapper.insert(bpo);
        if (result > 0) {
            return bpo.getId();
        }
        return null;
    }

    @Override
    public Long selectByName(String businessName) {
        log.info("持久层接受到查询商家名称：{}", businessName);
        QueryWrapper<BusinessPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_name", businessName);
        long count = businessMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public BusinessStoreVO selectByBusinessId(Integer businessId) {
        BusinessPO vo = businessMapper.selectById(businessId);
        BusinessStoreVO businessStoreVO = new BusinessStoreVO();
        
        // 从vo中取出数据给businessStoreVO赋值
        if (vo != null) {
            businessStoreVO.setId(vo.getId());
            businessStoreVO.setBusinessName(vo.getBusinessName());
            businessStoreVO.setIndustryType(vo.getIndustryType());
            businessStoreVO.setBusinessHeadName(vo.getBusinessHeadName());
            businessStoreVO.setBusinessHeadPhone(vo.getBusinessHeadPhone());
            businessStoreVO.setCompanyAddress(vo.getCompanyAddress());
            businessStoreVO.setBusinessLicense(vo.getBusinessLicense());
            businessStoreVO.setBusinessIntroduction(vo.getBusinessIntroduction());
            businessStoreVO.setEntryTime(vo.getEntryTime());
            businessStoreVO.setBusinessStatus(vo.getBusinessStatus());
            businessStoreVO.setAuditRemarks(vo.getAuditRemarks());
            businessStoreVO.setBusinessLogo(vo.getBusinessLogo());
        }
        return businessStoreVO;
    }
}